package com.ant.storage.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ant.storage.common.ResultPage;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.UserInfoDto;
import com.ant.storage.pojo.UserInfo;
import com.ant.storage.service.IUserInfoService;
import com.taobao.api.ApiException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author melon
 * @since 2023-07-17
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    @Value("${loginUrl.url}")
    private String Url;

    @Autowired
    IUserInfoService userInfoService;

    @ApiOperation(value = "获取员工列表")
    @GetMapping("/queryUserList")
    public ResultPage queryUserList(UserInfoDto dto) {
        return ResultPage.success("操作成功", userInfoService.getUserInfoPage(dto));
    }

    @ApiOperation(value = "更新员工列表")
    @PostMapping("/updateUser")
    public ResultVo updateUser(@RequestBody UserInfo userInfo) throws ApiException {
        userInfoService.userInfoUpdate(userInfo);
        return ResultVo.success("操作成功");
    }

    @ApiOperation(value = "绑定信息")
    @PostMapping("/save")
    public ResultVo save(@RequestBody UserInfo userInfo) throws ApiException {
        if (userInfo.getId() == null) {
            userInfo.setCreationTime(new Date());
            return ResultVo.success("操作成功", userInfoService.save(userInfo));
        } else {
            userInfo.setCreationTime(new Date());
            return ResultVo.success("操作成功", userInfoService.updateById(userInfo));
        }

    }

    @ApiOperation(value = "绑定信息")
    @GetMapping("/findUserById/{id}")
    public ResultVo findUserById(@PathVariable("id") Integer id) throws ApiException {
        UserInfo userInfo = userInfoService.getUserInfoById(id);
        return ResultVo.success("操作成功", userInfo);
    }


    @ApiOperation(value = "")
    @GetMapping("/getUserList")
    public ResultVo getUserList() throws Exception {
        String returnMsg = HttpUtil.get(Url + "/userSystem/user/findUserList?currentPage=1&pageSize=1000&status=true");
        JSONObject responseJsonObject = JSONUtil.parseObj(returnMsg);
        if (ObjectUtil.isNull(responseJsonObject)) {
            throw new Exception("响应异常:获取信息为空!");
        }
        String code = responseJsonObject.getStr("code");
        if (code.equals("200")) {
            String tableData = responseJsonObject.getStr("data");
            return ResultVo.success(responseJsonObject.getStr("msg"), JSONUtil.parseObj(tableData));
        }
        return ResultVo.fail(responseJsonObject.getStr("msg"));
    }

    @ApiOperation(value = "")
    @GetMapping("/findUsersByRoleIdsAndCityIds/{rid}/{id}")
    public ResultVo findUsersByRoleIdsAndCityIds(@PathVariable("rid") Integer rid,
                                                 @PathVariable("id") Integer id) throws Exception {
        String returnMsg = HttpUtil.get(Url + "/userSystem/user/findUsersByRoleIdsAndCityIds2?roleIds=" + rid +
                "&cityIds=" + id);
        JSONObject responseJsonObject = JSONUtil.parseObj(returnMsg);
        if (ObjectUtil.isNull(responseJsonObject)) {
            throw new Exception("响应异常:获取信息为空!");
        }
        String code = responseJsonObject.getStr("code");
        if (code.equals("200")) {
            String tableData = responseJsonObject.getStr("data");
            return ResultVo.success(responseJsonObject.getStr("msg"), JSONUtil.parseArray(tableData));
        }
        return ResultVo.fail(responseJsonObject.getStr("msg"));
    }

    @ApiOperation(value = "获取员工列表")
    @GetMapping("/findQueryUserByList")
    public ResultVo findQueryUserByList() {
        return ResultVo.success("操作成功", userInfoService.list());
    }

}
